package org.ilovecode.buttons
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import org.ilovecode.events.GUIEvent;
	
	
	public class NavigationButton extends Button
	{
		public static var ABOUT:String = "navigation button ABOUT";
		public static var APARTMENTS:String = "navigation button APARTMENTS";
		public static var BUSINESS:String = "navigation button BUSINESS";
		public static var GARAGE:String = "navigation button GARAGE";
		public static var LAYOUT:String = "navigation button LAYOUT";
		public static var SEARCH:String = "navigation button SEARCH";
		public static var FAQ:String = "navigation button FAQ";
		public static var CONTACT:String = "navigation button CONTACT";
		
		private const COLOR_ACTIVE:uint = 0xFFFFFF;
		private const COLOR_INACTIVE:uint = 0x000000;
		
		protected var _type:String;
		protected var _label:String;
		protected var _labelTextField:TextField;
		protected var _clickArea:MovieClip;
		
		public function NavigationButton(label:String = "", type:String = "", name:String="", enabled:Boolean=true)
		{
			super(name, enabled);
			
			_type = type;
			_label = label;
		}
		
		override protected function construct():void
		{
			super.construct();
			
			_labelTextField = TextField(this.getChildByName("label_txt"));
			_labelTextField.autoSize = TextFieldAutoSize.LEFT;
			_labelTextField.embedFonts = true;
			
			_clickArea = MovieClip(this.getChildByName("clickArea_mc"));
		}
		
		override protected function initText():void
		{
			super.initText();
			
			if (label == "")
				return;
			
			_labelTextField.text = _label;
		}
		
		override protected function initUI():void
		{
			super.initUI();
			
			_clickArea.width = _labelTextField.textWidth;
			_clickArea.height = _labelTextField.textHeight;
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			model.addEventListener(GUIEvent.SWITCH_SCREEN, update);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			model.removeEventListener(GUIEvent.SWITCH_SCREEN, update);
		}
		
		override protected function update(event:Event):void
		{
			switch(event.type)
			{
				case GUIEvent.SWITCH_SCREEN:
					if (model.currentScreen.type == type)
						_labelTextField.setTextFormat(new TextFormat(null, null, COLOR_ACTIVE));
					else
						_labelTextField.setTextFormat(new TextFormat(null, null, COLOR_INACTIVE));
					break;
				default:
					super.update(event);
					break;
			}
		}
		
		
		override protected function handle_Click(event:MouseEvent):void
		{
			if (_type != "")
				controller.showScreen(type);
		}
		
		override protected function handle_MouseOver(event:MouseEvent):void
		{
			super.handle_MouseOver(event);
			
			//If current screen is not assigned to this button, update it
			if (model.currentScreen.type != type)
				_labelTextField.setTextFormat(new TextFormat(null, null, COLOR_ACTIVE));
				
		}
		
		override protected function handle_MouseOut(event:MouseEvent):void
		{
			super.handle_MouseOut(event);
			
			//If current screen is not assigned to this button, update it
			if (model.currentScreen.type != type)
				_labelTextField.setTextFormat(new TextFormat(null, null, COLOR_INACTIVE));
		}
		
		public function get type():String
		{
			return _type;
		}

		public function set type(value:String):void
		{
			_type = value;
		}
		
		public function get label():String
		{
			return _label;
		}
		
		public function set label(value:String):void
		{
			_label = _labelTextField.text = value;
		}
		
	}
}